<?php
	include("gen.php");

	$cmd = get_datan("cmd");

	switch ($cmd) {
		case 1:
			//get one subdistrict based on id
			get_subdistrict();
			break;
		
		case 2:
			//get all details of all subdistricts
			get_subdistricts();
			break;

		case 3:
			//update a record data provided through the URL
			update_subdistrict();
			break;

		case 4:
			//delete a specific subdistrict record based on record id provide through the URL
			delete_subdistrict();
			break;

		case 5:
			# add a new subdistrict
			add_subdistrict();
			break;

		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
	}

	function get_subdistrict(){
		include_once("subdistrict_table_functions.php");

		//include_once("adb.php");

		$id = get_datan("id");

		$s = new subdistrict();

		$s->get_subdistrict($id);

		$row = $s->fetch();

		if(!$row){
			echo "{";
			echo jsonn("result",0).",";
			echo jsons("message","subdistrict not found");
			echo ",";
			return;
		}

		echo "{";
		echo jsonn("result", 1).",";
		echo '"subdistrict":{';
		echo jsonn("id",$id).",";
		echo jsons("subdistrictName", $row['subdistrict_name']).",";
		echo jsonn("districtId", $row['district_id']);
		echo "}";
		echo "}";
	}

	function get_subdistricts(){
		include_once("subdistrict_table_functions.php");
		include_once("adb.php");

		$v=new subdistrict();

		$v->list_all();

		$row= $v->fetch();

		if(!$row){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","vaccine not found");
			echo "}";
			return;
		}
		


		echo "{";
		echo jsonn("result",1) .",";
		echo '"subdistrict":[';

		while($row){
			echo "{";
			echo jsonn("id", $row['subdistrict_id']). ",";
			//name
			echo jsons ("subdistrictName", $row['subdistrict_name']). ",";
			//district
			echo jsons ("districtId", $row['district_id']);
			echo "}";
			$row= $v->fetch();
			
			if($row){
				echo ", ";
			}

		}
		echo "]";
		echo "}";
		

	}

	function update_subdistrict(){
		include_once("subdistrict_table_functions.php");

		$s = new subdistrict();

		$s_id = get_datan("id");

		$s_name = get_data("subd_name");

		$d_id = get_datan("dist_id");

		$s->edit_subdistrict($s_id, $s_name, $d_id);
	}

	function delete_subdistrict(){
		include_once("subdistrict_table_functions.php");

		$s = new subdistrict();

		$s_id = get_datan("id");

		$s->delete_subdistrict($s_id);

	}

	function add_subdistrict(){
		include_once("subdistrict_table_functions.php");

		$s = new subdistrict();

		$s_name = get_data("subd_name");

		$d_id = get_datan("dist_id");

		$s->add_subdistrict($s_name, $d_id);
	}
?>